package com.js.controller;


import com.js.pojo.Book;
import com.js.service.BookService;
import com.js.service.UserAndBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * Author：江松
 * Date：2023/4/25 8:53
 */
@Controller
@RequestMapping("/book")
public class BookController {


    @Autowired
    BookService bookService;
    @Autowired
    UserAndBookService userAndBookService;
    /**
     * 显示所有书目信息的接口
     * @param model
     * @return
     */
    @RequestMapping("/listBooks")
    public String listBooks(Model model){
        List<Book> books = bookService.listBooks();
//        for(Book book:books){
//            System.out.println(book.getBookid()+"--"+book.getBookname()+"--"+book.getBookcount());
//        }
        model.addAttribute("books", books);
        return "listBooks";
    }

    @GetMapping("/likeBooks")
    public String likeBooks(@RequestParam String subName,Model model){
        System.out.println(subName);
        System.out.println("***");
        subName="%"+subName+"%";
        List<Book>books=bookService.likeBooks(subName);
        model.addAttribute("books",books);
        return "listBooks";
    }

    /**
     * 根据id删除特定书目
     */
    @GetMapping("/delBook")
    public String delBook(@RequestParam("bookid") Integer bookid, Model model){
        int affected = bookService.delBool(bookid);
        if (affected > 0){
            model.addAttribute("msg", "成功删除id为 " + bookid + " 的书：");
        } else {
            model.addAttribute("msg", "删除失败");
        }
        return "information";
    }


    /**
     * 转到添加书目信息的界面
     * @return
     */
    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return "addBook";
    }

    @PostMapping("/addBook")
    public String addBook(Book book, Model model){
        // 查看是否存在同名书籍(不允许同名书籍存在)
        Book check = bookService.findBookByName(book.getBookname());
        if (check != null){
            model.addAttribute("msg", "添加失败, 已存在同名书籍");
        } else {
            int affected = bookService.addBook(book);
            if (affected > 0) {
                model.addAttribute("msg", "添加成功, 新书目的id为 " + book.getBookid());
            } else {
                model.addAttribute("msg", "添加失败");
            }
        }
        return "information";
    }


    /**
     * 转到更新书目信息
     */
    @RequestMapping("/toUpdateBook/{bookid}")
    public String toUpdateBook(Model model, @PathVariable Integer bookid){
        model.addAttribute("bookid", bookid);
        return "updateBook";
    }

    /**
     * 更新书目信息
     * @param book 需要更新的书的新信息
     * @param model
     * @return
     */
    @PostMapping("/updateBook")
    public String updateBook(Book book, Model model){
        int affected = bookService.updateBook(book);
        if (affected > 0){
            model.addAttribute("msg", "更新成功");
        } else {
            model.addAttribute("msg", "更新失败");
        }
        return "information";
    }


    /**
     * 用户借书
     * @param request json格式信息，包括bookid和userid
     * @return response json格式信息，包括info
     */
    @RequestMapping("/borrowBook")
    @ResponseBody
    public Map<String, String> borrowBook(@RequestBody Map<String, String> request){
        int bookid = Integer.parseInt(request.get("bookid"));
        int userid = Integer.parseInt(request.get("userid"));

        Map<String, String> response = new HashMap<String,String>(1);
        // 判断是否存在借过此书的记录(不允许借同一本)
        Integer ifBorrow = userAndBookService.checkIfBorrow(bookid, userid);
        if (ifBorrow != null){
            response.put("info", "借书失败, 你已借过此书");
        } else {
//            通过多个简单sql语句实现业务
//            bookService.borrowBook(bookid);
//            userAndBookService.borrowBook(bookid, userid);
            //通过存储过程实现
            try {
                bookService.borrowBookProc(bookid,userid);
            }catch (Exception e){
                System.out.println("使用存储过程借书发生异常！");
            }
            response.put("info", "借书成功");
        }
        return response;
    }



    /**
     * 用户还书
     */
    @RequestMapping("/returnBook")
    @ResponseBody
    public Map<String, String> returnBook(@RequestBody Map<String, String> request){
        int bookid = Integer.parseInt(request.get("bookid"));
        int userid = Integer.parseInt(request.get("userid"));
        Map<String, String> response = new HashMap<String, String>(1);


//        //mysql端创建了触发器，删除记录就会触发--对应书的库存+1
//        userAndBookService.returnBook(bookid, userid);
////        库存+1
////        bookService.returnBook(bookid);
        //借书存储过程
        try {
            bookService.returnBookProc(bookid,userid);
        }catch (Exception e){
            System.out.println("使用存储过程还书发生异常！");
        }
        response.put("info", "还书成功");
        return response;
    }



}
